﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XSTDZ.ERP.Core.Extentions;
using XSTDZ.ERP.Core.Generator;

namespace XSTDZ.ERP.Domain.Models
{
    /// <summary>
    /// 用户角色
    /// </summary>
   public partial class Role
    {
       public Role()
       {
           this.RolePermissions = new HashSet<RolePermission>();
           this.UserRoles = new HashSet<UserRole>();
       }

       /// <summary>
       /// 添加用户角色 
       /// </summary>
       /// <param name="permission">用户角色 </param>
       /// <returns>角色</returns>
       public Role AddPermission(RolePermission permission)
       {
           if (!this.RolePermissions.Contains(permission))
           {
               this.RolePermissions.Add(permission);
           }
           return this;
       }
       public Role ClearPermissions()
       {
           this.RolePermissions.Clear();
           return this;
       }
       public Role ClearUserRoles()
       {
           if (!this.UserRoles.Equals(null))
           {
               this.UserRoles.Clear();
           }
           return this;
       }
       public Role AddPermissions(IEnumerable<Guid> permissionIds)
       {
           if (permissionIds != null && permissionIds.Count() != 0)
           {
               this.RolePermissions.Clear();
               permissionIds.Each(permissionId => RolePermissions.Add(new RolePermission()
               {
                  Id= (Guid)new SequentialIdentityGenerator().Next,
                  PermissionID = permissionId,
                   RoleID=this.Id
               }));
           }
           return this;
       } 
     
       /// <summary>
       /// 添加用户角色
       /// </summary>
       /// <param name="permissions">用户角色集合</param>
       /// <returns>角色</returns>
       public Role AddPermissions(IEnumerable<RolePermission> permissions)
       {
           RolePermissions.Each(permission => this.AddPermission(permission));
           return this;
       }
       public Role AddPermissionIds(IEnumerable<Guid> rolePermissionIds)
       {
           
           rolePermissionIds.Each(rolePermissionId => this.AddPermission(
               new RolePermission
               { 
                     Id=Guid.NewGuid(),
                     RoleID=this.Id,
                     PermissionID = rolePermissionId
               }
               ));
           return this;
       } 
  /*
       /// <summary>
       /// 重置用户角色
       /// </summary>
       /// <param name="permissions">用户角色集合</param>
       /// <returns>角色</returns>
       public Role ResetPermissions(IEnumerable<RolePermission> permissions)
       {
           this.RolePermissions.Clear();
           return this.AddPermissions(permissions);
       }
        */
    }
}
